home *** CD-ROM | disk | FTP | other *** search
- extern "C" {
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- }
- #include "mask.h"
-
- void usage()
- {
- fprintf( stderr, "Usage: sendmail [-f file] [-C file] [user1] [user2] ...\n" );
- exit( 1 );
- }
-
-
- #define BUFFLEN 512
-
- FILE *infile = stdin;
- FILE *config = NULL;
- FILE *temp = NULL;
- char to[256], fileto[256];
- char mailfile[256];
- char buff[BUFFLEN];
-
-
- void quit( int code )
- {
- if( infile ) fclose( infile );
- if( config ) fclose( config );
- if( temp ) {
- fclose( temp );
- remove( mailfile );
- }
- exit( code );
- }
-
-
- char *get_fname( int *i, int argc, char **argv )
- {
- char *fname;
-
- if( argv[*i][2] ) return &argv[*i][2];
- if( (*i)+1 >= argc ) {
- fprintf( stderr, "sendmail: no file name given to %s option\n", argv[*i] );
- quit( 1 );
- }
- return argv[++(*i)];
- }
-
-
- void workfile()
- {
- int len;
-
- fileto[0] = 0;
- tmpnam( mailfile );
- temp = fopen( mailfile, "w" );
- if( ! temp ) {
- fprintf( stderr, "sendmail: unable to create temporary file\n" );
- quit( 1 );
- }
- while( fgets( buff, BUFFLEN-1, infile )) {
- len = strlen(buff)-1;
- while( buff[len]=='\r' || buff[len]=='\n' && len>=0 ) len--;
- buff[++len] = 0;
- if( ! *fileto && ! memcmp( buff, "To: ", 4 ) ) strcpy( fileto, buff+4 );
- fprintf( temp, "%s\n", buff );
- }
- fclose( temp );
- }
-
-
- void exe( class mskcmp *mask, char *com )
- {
- int i=0;
- char *next;
- char *last=com;
- char command[300];
- char *execomm=command;
- char *ins;
-
- *execomm = 0;
- while( 1 ) {
- next = strchr( last, '%' );
- if( next==0 || ! next[1] ) {
- memcpy( &execomm[i], last, strlen(last)+1 );
- execomm[strlen(last)+1+i] = 0;
- break;
- }
- memcpy( &execomm[i], last, next-last );
- i += next-last;
- switch( next[1] ) {
- case '%':
- execomm[i++] = '%';
- break;
- case 'f':
- memcpy( &execomm[i], mailfile, strlen(mailfile) );
- i += strlen(mailfile);
- break;
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- if( ! (ins = mask->getvar( next[1]-'0' ))) break;
- memcpy( &execomm[i], ins, strlen( ins ) );
- i += strlen( ins );
- break;
- case 't':
- memcpy( &execomm[i], to, strlen( to ) );
- i += strlen( to );
- break;
- default:
- memcpy( &execomm[i], next, 2 );
- i += 2;
- break;
- }
- last = next+2;
- }
- if( system(command) ) {
- fprintf( stderr, "sendmail: mail delivery failed\n" );
- exit( 1 );
- }
- }
-
-
-
- int sendmail()
- {
- class mskcmp *mask;
- char *com;
-
- if( ! *to ) {
- fprintf( stderr, "sendmail: mail file corrupt - no destination line\n" );
- quit( 1 );
- }
- fseek( config, 0, SEEK_SET );
- while( com=fgets( buff, BUFFLEN-1, config ) ) if( *buff!='#' ) {
- for( com=buff; *com && *com!=' '; com++ );
- if( ! *com ) continue;
- *(com++) = 0;
- mask = new mskcmp( buff );
- if( ! mask->isvalid( to ) ) {
- exe( mask, com );
- delete mask;
- break;
- }
- delete mask;
- }
- if( ! com ) {
- fprintf( stderr, "sendmail: no mail method for this destination\n" );
- quit( 2 );
- }
- return 0;
- }
-
-
- int main( int argc, char **argv )
- {
- int i;
- char *fname;
-
- for( i=1; i<argc && argv[i][0]=='-'; i++ ) {
- if( argv[i][0] != '-' ) usage();
- switch( argv[i][1] ) {
- case 'f':
- fname = get_fname( &i, argc, argv );
- if( !(infile = fopen( fname, "rb" )) ) {
- fprintf( stderr, "sendmail: file %s not found\n", fname );
- quit( 1 );
- }
- break;
-
- case 'C':
- fname = get_fname( &i, argc, argv );
- if( !(config = fopen( fname, "rb" )) ) {
- fprintf( stderr, "sendmail: configuration file %s not found\n", fname );
- quit( 1 );
- }
- break;
-
- default:
- fprintf( stderr, "Unknown option %s\n", argv[i] );
- usage();
- }
- }
- if( ! config ) {
- if( ! (fname = getenv( "sendmail$config" )) ) fname = "mailers";
- config = fopen( fname, "rb" );
- if( ! config ) {
- fprintf( stderr, "sendmail: configuration file %s not found\n", fname );
- quit( 1 );
- }
- }
- workfile();
- fclose( infile ); infile = NULL;
- if( i<argc ) for( ; i<argc; i++ ) {
- strcpy( to, argv[i] );
- sendmail();
- }
- else {
- char *next, *end;
-
- for( next=fileto; *next; ) {
- while( *next==' ' ) next++;
- if( ! *next ) break;
- end = strchr( next, ',' );
- if( ! end ) {
- end = next+strlen(next);
- end[1] = 0;
- }
- *end = 0;
- strcpy( to, next );
- for( i=strlen(to)-1; to[i]==' '; i-- ) to[i] = 0; /* 'rtrim' */
- sendmail();
- next = end+1;
- }
- }
- quit( 0 );
- }
-
-